package com.cat.slidingWindow;

import java.util.HashMap;

/**
 * @author cat
 * @description https://leetcode.cn/problems/1GxJYY/
 * @create 2025/7/31 20:29
 * @since JDK17
 */

public class Solution21 {
    static HashMap<Integer, Integer> map = new HashMap<>();
    static int N = ((int) (1e9 + 7));
    public int beautifulBouquet(int[] flowers, int k) {
        map.clear();
        long ans = 0;
        int cnt = 0, t, n = flowers.length;
        for (int l = 0, r = 0; r < n; r++) {    //
            cnt = map.getOrDefault(flowers[r], 0) + 1;  // 新增一朵花
            map.put(flowers[r], cnt);
            while (cnt > k) {
                t = map.get(flowers[l]) - 1;    // 取出当前位置
                cnt -= flowers[r] == flowers[l] ? 1 : 0;    // 相等减一个
                map.put(flowers[l++], t);
            }
            ans = ans + r - l + 1;
        }

        return ((int) (ans % N));
    }
}
